//
//  SuperviseReCheckDetailView.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/1/10.
//

import Foundation
import UIKit

class SuperviseReCheckDetailView: UIView {
    let submitBtn = UIButton.createPrimaryLarge("提交")
    let contentV = UIScrollView()
    
    var zgjls: [ZgjlParam] = []

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func reload(withModals modals: [SpRwbgtzModal?]) {
        contentV.removeAllSubViews()
        let role = UserDefaults.standard.string(forKey: SafetyUserRole)

        var lastI: SuperviseReCheckItemView?
        for (index, m) in modals.enumerated() {
            let itemV = SuperviseReCheckItemView()
            itemV.checkTF.isUserInteractionEnabled = (role == UserRole.SubBranchOffice.rawValue)
            itemV.didSelectItemWith = { (rwzt) -> () in
                var index = -1
                for (i, zg) in self.zgjls.enumerated() {
                    if zg.rwxid == m?.id {
                        index = i
                    }
                }
                
                let id = m?.zgjls?[0].id
                if index == -1 {
                    self.zgjls.append(ZgjlParam(id: id, rwxid: m?.id, rwjlid: m?.rwjlid, rwzt: rwzt))
                } else {
                    self.zgjls.remove(at: index)
                }
            }
            itemV.reload(withModal: m)
            contentV.addSubview(itemV)
            itemV.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.width.equalTo(ScreenWidth)
                make.top.equalTo(lastI == nil ? contentV.snp.top : lastI!.snp.bottom)
                if index == modals.count - 1 {
                    make.bottom.equalToSuperview().offset(-20)
                }
            }
            lastI = itemV
        }
    }
    
    func setupUI() {
        let role = UserDefaults.standard.string(forKey: SafetyUserRole)
        
        let isHide = role != UserRole.SubBranchOffice.rawValue
        if !isHide {
            addSubview(submitBtn)
            submitBtn.snp.makeConstraints { (make) -> Void in
                make.bottom.equalToSuperview().offset(-14)
                make.height.equalTo(50)
                make.centerX.equalTo(self.snp.centerX)
                make.width.equalTo(ScreenWidth - 20)
            }
        }
        
        contentV.keyboardDismissMode = .interactive
        contentV.showsVerticalScrollIndicator = false
        addSubview(contentV)
        contentV.snp.makeConstraints { (make) -> Void in
            make.top.equalToSuperview()
            make.centerX.equalTo(self.snp.centerX)
            make.width.equalTo(ScreenWidth)
            if isHide {
                make.bottom.equalTo(self.snp.bottom)
            } else {
                make.bottom.equalTo(submitBtn.snp.top).offset(-26)
            }
        }
     }
}


class SuperviseReCheckItemView: UIView, CheckEffectDelegate {
    func valueChange(_ isCheck: Bool?) {
        var rwzt = "1"
        if isCheck == true {
            rwzt = "2"
        } else if isCheck == false {
            rwzt = "3"
        }
        
        didSelectItemWith?(rwzt)
    }
    
    open var didSelectItemWith:((_ rwzt: String) -> ())?

    let checkTF = CheckEffect()
    let collectionV = SEImageCollectionView()
        
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func reload(withModal modal: SpRwbgtzModal?) {
        checkTF.title = modal?.rwmc ?? ""
        
        let zgjl = modal?.zgjls?[0]
        
        if (zgjl != nil) {
            msgL.text = zgjl?.tzsm
            var imgs: [String] = []
            let images = zgjl?.zgfjs ?? []
            for img in images {
                if img.fjdz != nil {
                    imgs.append(img.fjdz!)
                }
            }
            collectionV.imgs = imgs
        }
    }
    
    func setupUI() {
        checkTF.delegate = self
        addSubview(checkTF)
        checkTF.snp.makeConstraints { (make) -> Void in
            make.centerX.equalToSuperview()
            make.width.equalTo(ScreenWidth - 20)
            make.top.equalToSuperview().offset(10)
        }
        
        addSubview(msgL)
        msgL.snp.makeConstraints { (make) -> Void in
            make.centerX.equalToSuperview()
            make.width.equalTo(ScreenWidth - 20)
            make.top.equalTo(checkTF.snp.bottom).offset(10)
        }
        
        collectionV.itemHeight = 70
        collectionV.maxCount = 4
        addSubview(collectionV)
        collectionV.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(13)
            make.right.equalToSuperview().offset(-13)
            make.top.equalTo(msgL.snp.bottom).offset(16)
            make.bottom.equalToSuperview()
        }
    }
    
    lazy var msgL: PaddingLabel = {
        let l = PaddingLabel()
        l.insets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        l.backgroundColor = .white
        l.layer.masksToBounds = true
        l.layer.cornerRadius = 5
        l.layer.borderWidth = 0.5
        l.layer.borderColor = UIColor.hex("#DEDEDE").cgColor
        l.layer.backgroundColor = UIColor.white.cgColor
        l.font = .systemFont(ofSize: 14)
        l.textColor = .hex("#333333")
        return l
    }()
}
